package com.leoman.userchange.dao;

import com.leoman.common.dao.IBaseJpaRepository;
import com.leoman.userchange.entity.UserChange;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * 用户操作dao
 * Created by 涂奕恒 on 2016/6/12.
 */
public interface UserChangeDao extends IBaseJpaRepository<UserChange> {

    @Query("select a from UserChange a where a.postBase.id = ?1 and a.type = ?2 and a.userInfo.id = ?3")
    List<UserChange> findList(Long sourceId, Integer type, Long userId);

    @Query("select a from UserChange a where a.postBase.id = ?1 and a.type = ?2")
    List<UserChange> findList(Long sourceId, Integer type);

    @Query("select a from UserChange a where a.postBase.userInfo.id = ?1 and a.type = ?2 and a.postBase.deleteStatus <> 1 order by a.createDate desc")
    Page<UserChange> findListByUserId(Long userId, Integer type, Pageable pageable);

    @Query("select a from UserChange a where a.userInfo.id = ?1 and a.type = ?2 and a.postBase.deleteStatus <> 1 order by a.createDate desc")
    Page<UserChange> iPageByParams(Long userId, Integer type, Pageable pageable);

    @SuppressWarnings("unused")
    @Query("select a from UserChange a where a.userInfo.id = ?1 and a.type = ?2 and a.postBase.deleteStatus <> 1")
    List<UserChange> iFindListByParams(Long userId, Integer type);

    @Query("select a from UserChange a where a.postBase.userInfo.id = ?1")
    List<UserChange> iFindListByParams(Long userId);

    @Query("select a from UserChange a where a.userInfo.id = ?1 and a.postBase.id = ?2 and a.type = ?3")
    List<UserChange> findListByParams(Long userId, Long sourceId, Integer type);

    // 用户当天点赞帖子的次数
    @SuppressWarnings("unused")
    @Query("SELECT COUNT(a) FROM UserChange a WHERE a.userInfo.id = ?1 AND a.createDate >= ?2 AND a.createDate <?3 AND a.type = 1 ")
    Integer findTodayPraiseNum(Long userId, Long morningDate, Long nightDate);

    @Query("select a from UserChange a where a.postBase.id = ?1 and a.postBase.deleteStatus <> 1 and a.type = 1")
    List<UserChange> findByPostId(Long postId);
}
